<!--商学院主体列表 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="主体列表" name="one">
        <SearchTable v-model:formStatus="formStatus" ref="searchTableRef" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="主体详情" name="two">
        <FormTab v-model:formStatus="formStatus" v-model:form="form" ref="formRef" @handleInitData="clearData" @handleSuccess="handleSuccess" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="businessSchoolsClassInfo">
import SearchTable from "@/components/businessSchoolManager/businessSchoolEntity/SearchTable.vue";
import FormTab from "@/components/businessSchoolManager/businessSchoolEntity/FormTab.vue";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { TabsPaneContext } from "element-plus";
import type { ISchoolEntity, ISchoolEntityDTO } from "@/types/businessSchoolManager/businessSchoolEntity";

/* 表格状态 */
const formStatus = ref<ViewMode>("none");
const activeName = ref<TabsMode>("one");

/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");
const formRef = ref<InstanceType<typeof FormTab>>();

const form = reactive<ISchoolEntityDTO>({
  id: 0,
  name: "",
  is_show: "",
  title: "",
  sort: 0
});

/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}
/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}
/* 点击编辑按钮 */
function handleViewEdit(row: ISchoolEntity, code: ViewModeFilter) {
  formStatus.value = code;
  activeName.value = "two";
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    form.id = row.id;
    form.is_show = row.is_show;
    form.title = row.title;
    form.sort = row.sort;
    form.name = row.name;
  });
}
</script>

<style lang="scss" scoped></style>
